package com.iot.inter.controller.newinterface.extra;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newrequest.extra.extraorder.request.RequestOrderExtraPackages;
import com.iot.inter.entity.newrequest.extra.extraorder.response.ResponseOrderExtraPackages;
import com.iot.inter.interfaceprocessing.newinterface.TurnToExtraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：3.13订购加油包:2.0接口
 * <p>
 * 接入方可以通过调用该接口，可以为某个订单订购加油包；
 * 订购加油包时，首先判断订单关联的套餐是否是预付费的，而且订单还没有过期。
 * 只有对于预付费的且未过期的订单，才允许订购加油包。如果套餐是后付费的：不允许订购加油包。
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.13订购加油包")
public class ExtraPackagesOrderController {
    private static Logger logger = Logger.getLogger(ExtraPackagesOrderController.class);

    @CrossOrigin
    @RequestMapping(value = "extraPackageOrder", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.13订购加油包", response = ResponseOrderExtraPackages.class)
    public String extraPackageOrder(@ApiParam @RequestBody RequestOrderExtraPackages request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {

            logger.info("【订购加油包】请求：" + requestStr);

            RequestOrderExtraPackages requestPackage = JSONArray.parseObject(requestStr, RequestOrderExtraPackages.class);
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();
            String orderId = requestPackage.getOrderId();
            String refpackageFlow = requestPackage.getRefpackageFlow();
            String isOpen = requestPackage.getIsOpen();

            //3.13	订购加油包
            TurnToExtraService extra = new TurnToExtraService();
            OrderPaymentManager response = extra.extraPackageOrder(orderId, refpackageFlow, isOpen, secret, partnerCode);

            if (response != null) {

                ResponseOrderExtraPackages res = new ResponseOrderExtraPackages();
                res.setRetCode(response.getRetCode());
                res.setResultInfo(response.getResultInfo());
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【订购加油包】成功响应：" + jsonString);
                return jsonString;


            } else {
                ResponseOrderExtraPackages res = new ResponseOrderExtraPackages();
                res.setRetCode("9998");
                res.setResultInfo("响应报文为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【订购加油包】响应：" + jsonString);
                return jsonString;

            }

        } else {

            ResponseOrderExtraPackages res = new ResponseOrderExtraPackages();
            res.setRetCode("9998");
            res.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(res);
            logger.info("【订购加油包】响应" + jsonString);
            return jsonString;

        }
    }
}
